package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Material;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Unit;
import com.qidianit.mes.pojo.WorkflowProduct;
import com.qidianit.mes.pro.mapper.MaterialMapper;
import com.qidianit.mes.pro.mapper.UnitMapper;
import com.qidianit.mes.pro.mapper.WorkflowProductMapper;
import com.qidianit.mes.pro.service.WorkflowProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/19 -16:14
 */
@Service
public class WorkflowProductServiceImpl implements WorkflowProductService {

    @Autowired
    private WorkflowProductMapper workflowProductMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UnitMapper unitMapper;
    @Override
    public Result<PageInfo<WorkflowProduct>> queryList(Long workflowId,int page,int size) {
        //分页
        PageHelper.startPage(page,size);
        //查询数据
        List<WorkflowProduct> workflowProducts = workflowProductMapper.queryList(workflowId);
        if (!workflowProducts.isEmpty()){
            for (WorkflowProduct workflowProduct : workflowProducts) {
                Material material = materialMapper.queryByCode(workflowProduct.getProductCode());
                workflowProduct.setProduct(material);
                Unit unit = unitMapper.queryById(material.getUnitId());
                workflowProduct.getProduct().setUnit(unit);

            }
        }

        return Result.success(new PageInfo<>(workflowProducts));
    }

    @Override
    public Result saveWorkflowProduct(WorkflowProduct workflowProduct) {
        //同一个工艺中产品不能重复
        List<WorkflowProduct> workflowProducts = workflowProductMapper.queryList(workflowProduct.getWorkflowId());
        if (!workflowProducts.isEmpty()){
            for (WorkflowProduct product : workflowProducts) {
                if (product.getProductCode().equals(workflowProduct.getProductCode())){
                    return Result.error("该产品/物料已经添加");
                }
            }
        }
        return workflowProductMapper.saveWorkflowProduct(workflowProduct)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result<WorkflowProduct> queryById(Long id) {
        WorkflowProduct workflowProduct = workflowProductMapper.queryWorkflowProductById(id);
        workflowProduct.setProduct(materialMapper.queryByCode(workflowProduct.getProductCode()));
        workflowProduct.getProduct().setUnit( unitMapper.queryById(workflowProduct.getProduct().getUnitId()));
        return Result.success(workflowProduct);
    }

    @Override
    public Result updateById(WorkflowProduct workflowProduct) {
        //同一个工艺中产品不能重复
        List<WorkflowProduct> workflowProducts = workflowProductMapper.queryList(workflowProduct.getWorkflowId());
        if (!workflowProducts.isEmpty()){
            for (WorkflowProduct product : workflowProducts) {
                if (product.getProductCode().equals(workflowProduct.getProductCode())&&
                !product.getId().equals(workflowProduct.getId())){
                    return Result.error("该产品/物料已经添加");
                }
            }
        }

        return workflowProductMapper.updateWorkflowProduct(workflowProduct)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result deleteById(Long id) {
        return workflowProductMapper.deleteWorkflowProductById(id)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return workflowProductMapper.batchDeleteWorkflowProduct(ids)>0?Result.success():Result.error("操作失败");
    }
}
